/**
 * 客户联系人
 * Created by wenchuan.li on 2018/6/14 11:16.
 */
define(function () {

    var self,
        app = win.app,
        orderSubmitArg, GWAK001,
        searchData = {
            keyword: ''
        },
        addr={
            oftenCount:0,
            seq:''
        }
    ;

    var Customers = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _refreshAutoInit: true //自动加载刷新控件
            }, win);
            orderSubmitArg = self._pageParameters;
            orderSubmitArg = app.pageParameters['khlxr_page'];
            GWAK001 = app.cloneJSON(orderSubmitArg.item);
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll1'];
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat900/getGwalList",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWAK001: GWAK001.GWAK001,
                    keyword: searchData.keyword
                },
                cb: function (result) {
                    if (result.ok) {
                        //goods.data.length = 0;
                        var data = result.data;
                        goods.data.pushArray(data.data, goods.page == 0);

                        _super({
                            obj: goods,
                            dataLen: data.data.length,
                            nodata: '无客户联系人信息...'
                        });
                    }
                }
            });
        },
        _search: function (data) {
            searchData.keyword = data.keyword.trim();
            self._refresh();
        },
        getAddr: function () {

            app.ajax({
                xurl: "api/wx/gwat900/getAddr",
                xdata: {
                    GWAK001: GWAK001.GWAK001
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;

                        addr.oftenCount = data.length;

                        if (addr.oftenCount>0){
                            addr.seq = data[0].GWAL002;
                        }
                    }
                }
            });
        },
        _lazy: function () {
            this._super();

            self.getAddr();
            app.taps.call(win, [
                {
                    o: 'addBtn',
                    cb: function (e) {
                        app.openPage({
                            src: "khxx/khlxr-info.html",
                            argsKey: 'khlxrinfo_page',
                            args: {
                                type: 'add',
                                item: app.cloneJSON(GWAK001),
                                addr:addr,
                                cb: function () {
                                    self.getAddr();
                                    self._refresh();
                                }
                            }
                        });
                    }
                }
            ]);
        },
        _ready: function () {
            Vue.component('khlxritem', {
                template: app.getByid(doc, 'khlxr-template'),
                props: {
                    model: Array
                },
                methods: {
                    select: function (v) {
                        app.openPage({
                            src: "khxx/khlxr-info.html",
                            argsKey: 'khlxrinfo_page',
                            args: {
                                type: 'edit',
                                item: app.cloneJSON(v),
                                addr:addr,
                                cb: function () {
                                    self.getAddr();
                                    self._refresh();
                                }
                            }
                        });
                    },
                    deleteItem: function (item, _node) {
                        app.ajax({
                            xurl: "api/wx/gwat900/delGwal",
                            xdata: {
                                GWAL001: item.GWAL001,
                                GWAL002: item.GWAL002
                            },
                            confirm: {
                                msg: '是否删除客户联系人？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.alert({
                                        msg: '客户联系人删除成功'
                                    });
                                    self.getAddr();
                                    self._getRefreshObj().data.remove(item);
                                    mui.swipeoutClose($(_node.target).parents("li").get(0));
                                }
                            }
                        });
                    }
                }
            });
        }
    });

    new Customers();

});